<html>
<header>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{
	font-size:1rem;
}
</style>
</header>
<body style='height:100%;display:block'>
<div style='height:100%;padding:1rem'>
	<div>
		<div  style='display:inline-block;width:49%;float:left' >
			<div >
			持仓:<input readOnly type='text' style='width:5rem;border:0px;padding:1px 5px' id='cm' value=0>
			</div>
			<div>
				金额:<input type='text' style='width:5rem;border:0px;padding:1px 5px' id='mn' value=5000>
			</div>
			<div>总价:<span id='zj'></span></div>
			<div>天数:<span id='ts'></span></div>
			<div>日利率:<span id='dayRat'></span></div>
		</div>
		<div style='display:inline-block;width:49%;float:right'>
			<div>操盘数:<span id='cps'></span></div>
			<div>每日操盘平均:<span id='cpsrat'></span></div>
			<div>平均成本:<span id='avgcb'></span></div>
			<div>操盘盈亏:<span id='optyk'></span></div>
			<div>年利率:<span id='yearRat'></span></div>
		</div>
		
	</div>
	<div>
		<div style='display:inline-block;width:49%;float:left'>
			<span>价格&nbsp;数量</span>
			<textarea style='width:100%;border:0px;height:50%;display:block' id='line'></textarea>
		</div>
		<div style='height:50%;display:inline-block;width:49%;float-right;'>
			<div>操作历史</div>
			<div id='history' style='font-size:0.7rem;height:100%;overflow:auto'>
				
			</div>
		</div>
	</div> 
	<button id='enter' style='width:100%;padding:0.5rem'>确定</button>
</div>
<script>
	var dayOfTimes = 15 , dayOfOpt = dayOfTimes , startAmount = 0 ,
		days = 1 , opts = 0 , totalNum = 0 , totalAmount = 0 , yk = 0 ,
		curOfTimes = 0 , curOfDay = 0 , curAmount = 0 , rate = 0 ,
		startTotal  ;

	var yinHua = 0.0001 , guanLi = 0.00025 , yingJin = 5 , guoHu = 0.00002;
	function init(){

	}
	function getOut( totalAmount , type ){
		totalAmount = Math.abs( totalAmount ) ;
		var fee = totalAmount * yinHua  + totalAmount * guanLi + totalAmount * guoHu ;
		if( type > 0 ) return fee + 5 ;
		else if( type < 0 ) return fee + 5;
		else return 0 ;
	}
	function resetAmount( p ){
		curAmount = (p * parseInt(id('cm').value * 100 ) + parseFloat(id('mn').value ) ).toFixed(2) ;
		id('zj').innerHTML = curAmount ;
	}
	function id( id ){
		return document.getElementById( id ) ;
	}
	function clear(){
		var lines = id( 'line' ).value.split('\n') , newLines = ''  ;
		for( var key in lines ){
			if( !/^([0-9]+(\.[0-9]{1,2})?)\s*([+-]?[0-9]+)?$/.test( lines[key] ) ){
				continue ;
			}
			newLines += lines[key] + '\n' ;
		}
		id( 'line' ).value = newLines ;
	}
	function caculate(){
		if( typeof startTotal == 'undefined' ){
			startTotal = parseFloat( id('mn').value ) ;
		}
		//clear() ;
		var lines = id( 'line' ).value.split('\n') ;
		if( !lines.length ) return ;
		var ptn = /^([0-9]+(\.[0-9]{1,2})?)\s+([+-]?[0-9]+)$/.exec( lines[lines.length-1]) ;
			var p , h , th , tp , isNext = true , ta = 0 , sa = 0 ;
		if( ptn ) {
			p = parseFloat(ptn[1]) ; 
			h = parseInt(ptn[3]) ;
			th = parseInt( id('cm').value ) ; 
			tp = parseFloat( id('mn') .value ) ;
			th += h;
			ta = p * h * 100 * -1 ;
			var out = getOut( ta , h ) ;
			sa = ta - out ;
			tp += sa ;
			
			if( th < 0 || tp < 0) isNext = false ;
			if( ! isNext ) return ;
			else {
				id('cm').value = th ;
				id('mn').value = tp.toFixed(2) ;
			}
			//计算平均成本
			if( h > 0 ){
				totalNum += h * 100 ;
				totalAmount += p * h * 100 ;
				totalAmount += out ;
				id('avgcb').innerHTML = (totalAmount/totalNum).toFixed(2) ;
			}else if( h < 0 ){//计算浮动盈亏
				yk +=(  (p + out/(h*100) ) - totalAmount / totalNum ) * h * 100 * -1;
				id('optyk').innerHTML = yk.toFixed(2) ;
				var dayRat =   ( yk / days / startTotal  ) * 100 ,
				yearRat = dayRat * 200 ;
				id('dayRat').innerHTML = dayRat.toFixed(4) +'%';
				id('yearRat').innerHTML = yearRat.toFixed(2) +'%';
			}
			//日志
			var msg = h>0 ? '买入' : '卖出' ;
			var text = msg + ':' + '总价:' + Math.abs(ta).toFixed(2) + '/手续' + getOut(ta , h).toFixed(4) +'/实价:' + Math.abs(sa).toFixed(2) ;
			log( text ) ;
			
		}
		if( isNext ) next() ;
	}

	function log( text ){
		id('history').innerHTML += text + '<br>' ;
		id('history').scrollTop = id('history').scrollHeight ;
	}

	function next(){
		var lines = id( 'line' ).value.split('\n') ;
		var ptn = /^([0-9]+(\.[0-9]{1,2})?)\s*([+-]?[0-9]+)?$/.exec( lines[lines.length-1]) ;
		var p = parseFloat(ptn[1]) ;
		
		p += getOffset() ;
		if( lines.length >= 15 ) id( 'line' ).value = '' ;
		id('line').value += '\n'+p.toFixed(2)+' ' ;
		resetAmount( p ) ;
		id('line').scrollTop = id('line').scrollHeight ;

		if(ptn[3]){
			id('cps').innerHTML = ++opts ;
		}
		id('cpsrat').innerHTML = days == 0  ? opts : (opts / days ).toFixed(2) ;
		
	}

	function getOffset(){
		var d = 0.01;
		if( Math.random() >= 0.5 ) d *= -1 ;
		return parseFloat((d * getOpen()).toFixed(2)) ;
	}

	function getOpen(){
		dayOfOpt -- ;
		var offset = 1 ;
		if( dayOfOpt > 0 ) 
			return offset ;
		else 
			offset = Math.round( Math.random() * 3 + 1 ) ;
		dayOfOpt = dayOfTimes ;
		id('ts').innerHTML = ++days ;
		return Math.random() >= 0.5 ? offset *= -1 : offset ;
	}

	id('line').onkeydown = function( e ){
		if( e.keyCode == 13 ){
			caculate() ;
			return false ;
		}
	}

	id('enter').onclick=function(){
		caculate() ;
	}

</script>

</body>
</html>